package org.hot100.h001;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wjy
 * @Date: 2021/12/3 11:06
 */
public class Solution {

    public static void main(String[] args) {

        //int[] ints = {2, 7, 18, 30, 11, 15};
        int[] ints = {1, 2, 7, 18, 5, 4, 6, 16};

        Solution solution = new Solution();
        int[] ints1 = solution.twoSum(ints, 9);

        for (int i : ints1) {
            System.out.println(i);
        }

    }

    /**
     * 绝！
     * @param nums
     * @param target
     * @return
     */
    public int[] twoSum(int[] nums, int target) {

        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[] {map.get(target - nums[i]), i};
            }
            map.put(nums[i], i);
        }

        return new int[0];
    }

}
